package com.mine.frame.mylandemo

import com.blankj.utilcode.util.ConvertUtils
import com.mine.frame.mylandemo.utils.*
import com.swallowsonny.convertextlibrary.readInt16BE
import com.swallowsonny.convertextlibrary.readUInt16BE
import com.swallowsonny.convertextlibrary.toHexString
import com.third.device.mg.serialport.utils.*
import org.junit.Assert.assertEquals
import org.junit.Test

/**
 * Example local unit test, which will execute on the development machine (host).
 *
 * See [testing documentation](http://d.android.com/tools/testing).
 */
class ExampleUnitTest {
    @Test
    fun addition_isCorrect() {
        assertEquals(4, 2 + 2)
    }


    @Test
    fun testCrc(){
        val command = ByteArray(6)
        command[0] = 0x01
        command[1] = com.third.device.mg.serialport.factory.forick.BaseForickProduct.WRITE_SINGLE_MODE.toByte()
        System.arraycopy(com.third.device.mg.serialport.factory.forick.product.PanelUART.HLR_ON_OFF_AIR,0 ,command ,2 ,2)
        command[4] = 0x00.toByte()
        command[5] = 0x01.toByte()
        val result = com.third.device.mg.serialport.factory.forick.ForickCRC.crc16LowBefore(command)
        print(ConvertUtils.int2HexString(result).toUpperCase())
    }

    @Test
    fun testConvertUtils(){
        println("========Int 转 byte[4] 默认格式:低位在前，高位在后================================================")
        println("0:" +MyConvertUtils.intTo4BytesLowHigh(0).toHexString() )
        println("1:" +MyConvertUtils.intTo4BytesLowHigh(1).toHexString() )
        println("2:" +MyConvertUtils.intTo4BytesLowHigh(2).toHexString() )
        println("3:" +MyConvertUtils.intTo4BytesLowHigh(3).toHexString() )
        println("10:" +MyConvertUtils.intTo4BytesLowHigh(10).toHexString() )
        println("11:" +MyConvertUtils.intTo4BytesLowHigh(11).toHexString() )
        println("12:" +MyConvertUtils.intTo4BytesLowHigh(12).toHexString() )
        println("13:" +MyConvertUtils.intTo4BytesLowHigh(13).toHexString() )
        println("14:" +MyConvertUtils.intTo4BytesLowHigh(14).toHexString() )
        println("15:" +MyConvertUtils.intTo4BytesLowHigh(15).toHexString() )
        println("16:" +MyConvertUtils.intTo4BytesLowHigh(16).toHexString() )
        println("500:" +MyConvertUtils.intTo4BytesLowHigh(500).toHexString() )
        println("1024:" +MyConvertUtils.intTo4BytesLowHigh(1024).toHexString() )
        println("2048:" +MyConvertUtils.intTo4BytesLowHigh(2048).toHexString() )


        println("==========Int 转 byte[4] 自定义格式:高位在前，低位在后==============================================")
        println("0:" +MyConvertUtils.intTo4BytesHighLow(0).toHexString() )
        println("1:" +MyConvertUtils.intTo4BytesHighLow(1).toHexString() )
        println("2:" +MyConvertUtils.intTo4BytesHighLow(2).toHexString() )
        println("3:" +MyConvertUtils.intTo4BytesHighLow(3).toHexString() )
        println("10:" +MyConvertUtils.intTo4BytesHighLow(10).toHexString() )
        println("11:" +MyConvertUtils.intTo4BytesHighLow(11).toHexString() )
        println("12:" +MyConvertUtils.intTo4BytesHighLow(12).toHexString() )
        println("13:" +MyConvertUtils.intTo4BytesHighLow(13).toHexString() )
        println("14:" +MyConvertUtils.intTo4BytesHighLow(14).toHexString() )
        println("15:" +MyConvertUtils.intTo4BytesHighLow(15).toHexString() )
        println("16:" +MyConvertUtils.intTo4BytesHighLow(16).toHexString() )
        println("500:" +MyConvertUtils.intTo4BytesHighLow(500).toHexString() )
        println("1024:" +MyConvertUtils.intTo4BytesHighLow(1024).toHexString() )
        println("2048:" +MyConvertUtils.intTo4BytesHighLow(2048).toHexString() )

        println("===========Int 转 byte[2] 默认格式:低位在前，高位在后=============================================")
        println("0:" +MyConvertUtils.intTo2BytesLowHigh(0).toHexString() )
        println("1:" +MyConvertUtils.intTo2BytesLowHigh(1).toHexString() )
        println("2:" +MyConvertUtils.intTo2BytesLowHigh(2).toHexString() )
        println("3:" +MyConvertUtils.intTo2BytesLowHigh(3).toHexString() )
        println("10:" +MyConvertUtils.intTo2BytesLowHigh(10).toHexString() )
        println("11:" +MyConvertUtils.intTo2BytesLowHigh(11).toHexString() )
        println("12:" +MyConvertUtils.intTo2BytesLowHigh(12).toHexString() )
        println("13:" +MyConvertUtils.intTo2BytesLowHigh(13).toHexString() )
        println("14:" +MyConvertUtils.intTo2BytesLowHigh(14).toHexString() )
        println("15:" +MyConvertUtils.intTo2BytesLowHigh(15).toHexString() )
        println("16:" +MyConvertUtils.intTo2BytesLowHigh(16).toHexString() )
        println("4660:" +MyConvertUtils.intTo2BytesLowHigh(4660).toHexString() )
        println("0x1234:" +MyConvertUtils.intTo2BytesLowHigh(0x1234).toHexString() )



        println("=========byte[2]低位在前，高位在后  转 Int===============================================")
        val b10 = MyConvertUtils.intTo2BytesLowHigh(0)
        val b11 = MyConvertUtils.intTo2BytesLowHigh(1)
        val b12 = MyConvertUtils.intTo2BytesLowHigh(12)
        val b13 = MyConvertUtils.intTo2BytesLowHigh(13)
        val b14 = MyConvertUtils.intTo2BytesLowHigh(14)
        val b1024 = MyConvertUtils.intTo2BytesLowHigh(1024)
        val b2048 = MyConvertUtils.intTo2BytesLowHigh(2048)
        val b4660 = MyConvertUtils.intTo2BytesLowHigh(4660)
        println(b10.toHexString() + " ==>> " +MyConvertUtils.twoBytesToInt(b10[0] ,b10[1]) )
        println(b11.toHexString() + " ==>> " +MyConvertUtils.twoBytesToInt(b11[0] ,b11[1]) )
        println(b12.toHexString() + " ==>> "  +MyConvertUtils.twoBytesToInt(b12[0] ,b12[1]) )
        println(b13.toHexString() + " ==>> "  +MyConvertUtils.twoBytesToInt(b13[0] ,b13[1]) )
        println(b14.toHexString() + " ==>> "  +MyConvertUtils.twoBytesToInt(b14[0] ,b14[1]) )
        println(b1024.toHexString() + " ==>> "  +MyConvertUtils.twoBytesToInt(b1024[0] ,b1024[1]) )
        println(b2048.toHexString() + " ==>> "  +MyConvertUtils.twoBytesToInt(b2048[0] ,b2048[1]) )
        println(b4660.toHexString() + " ==>> "  +MyConvertUtils.twoBytesToInt(b4660[0] ,b4660[1]) )
    }

    @Test
    fun testMyConvertUtils(){
        val b1 = MyConvertUtils.intTo2BytesLowHigh(0x0001)
        val b2 = MyConvertUtils.intTo2BytesLowHigh(0x0101)
        val b3 = MyConvertUtils.intTo2BytesLowHigh(0x0002)
        val b4 = MyConvertUtils.intTo2BytesLowHigh(0x0102)
        val b5 = MyConvertUtils.intTo2BytesLowHigh(0x0003)
        val b6 = MyConvertUtils.intTo2BytesLowHigh(0x0103)
        val b7 = MyConvertUtils.intTo2BytesLowHigh(0x0203)
        val b8 = MyConvertUtils.intTo2BytesLowHigh(0x0303)
        println("0x0001 => (Hex): " +b1.toHexString() + " (Int): " + MyConvertUtils.twoBytesToInt(b1[0] ,b1[1]))
        println("0x0101 => (Hex): " +b2.toHexString() + " (Int): " + MyConvertUtils.twoBytesToInt(b2[0] ,b2[1]))
        println("0x0002 => (Hex): " +b3.toHexString() + " (Int): " + MyConvertUtils.twoBytesToInt(b3[0] ,b3[1]))
        println("0x0102 => (Hex): " +b4.toHexString() + " (Int): " + MyConvertUtils.twoBytesToInt(b4[0] ,b4[1]))
        println("0x0003 => (Hex): " +b5.toHexString() + " (Int): " + MyConvertUtils.twoBytesToInt(b5[0] ,b5[1]))
        println("0x0103 => (Hex): " +b6.toHexString() + " (Int): " + MyConvertUtils.twoBytesToInt(b6[0] ,b6[1]))
        println("0x0203 => (Hex): " +b7.toHexString() + " (Int): " + MyConvertUtils.twoBytesToInt(b7[0] ,b7[1]))
        println("0x0303 => (Hex): " +b8.toHexString() + " (Int): " + MyConvertUtils.twoBytesToInt(b8[0] ,b8[1]))

    }

    @Test
    fun test2(){
        val b1 = MyConvertUtils.intTo2BytesLowHigh(0x0100)
        val b2 = MyConvertUtils.intTo2BytesLowHigh(0x0101)
        val b3 = MyConvertUtils.intTo2BytesLowHigh(0x0200)
        val b4 = MyConvertUtils.intTo2BytesLowHigh(0x0201)
        val b5 = MyConvertUtils.intTo2BytesLowHigh(0x0300)
        val b6 = MyConvertUtils.intTo2BytesLowHigh(0x0301)
        val b7 = MyConvertUtils.intTo2BytesLowHigh(0x0302)
        val b8 = MyConvertUtils.intTo2BytesLowHigh(0x0303)
        println("0x0100 => (Hex): " +b1.toHexString() + " (Int): " + MyConvertUtils.twoBytesToInt(b1[0] ,b1[1]))
        println("0x0101 => (Hex): " +b2.toHexString() + " (Int): " + MyConvertUtils.twoBytesToInt(b2[0] ,b2[1]))
        println("0x0200 => (Hex): " +b3.toHexString() + " (Int): " + MyConvertUtils.twoBytesToInt(b3[0] ,b3[1]))
        println("0x0201 => (Hex): " +b4.toHexString() + " (Int): " + MyConvertUtils.twoBytesToInt(b4[0] ,b4[1]))
        println("0x0300 => (Hex): " +b5.toHexString() + " (Int): " + MyConvertUtils.twoBytesToInt(b5[0] ,b5[1]))
        println("0x0301 => (Hex): " +b6.toHexString() + " (Int): " + MyConvertUtils.twoBytesToInt(b6[0] ,b6[1]))
        println("0x0302 => (Hex): " +b7.toHexString() + " (Int): " + MyConvertUtils.twoBytesToInt(b7[0] ,b7[1]))
        println("0x0303 => (Hex): " +b8.toHexString() + " (Int): " + MyConvertUtils.twoBytesToInt(b8[0] ,b8[1]))
    }

    @Test
    fun bytesToInt(){
        val num = "3412"
        val numBytes = num.hexToBytes()
        println("bytes[]       ==> " + numBytes.toHexString())
        println("twoBytesToInt ==> " + MyConvertUtils.twoBytesToInt(numBytes[0] ,numBytes[1]))
        println("toUInt16      ==> " + numBytes.toUInt16L())
    }


    @Test
    fun testValve(){
        val panel = com.third.device.mg.serialport.factory.forick.product.PanelUART()
        //两路双线阀 冷阀开
        //01 06 00 05 03 01 D8 5A
        val hex = panel.changeValveModeAir(1.toByte() ,3 ,1).toHexString()
        println("command ==> $hex")
    }

    @Test
    fun testToByte(){
        for (index in 5 .. 50){
            val bIndex = index.to2ByteH()
            println("$index hex ==> ${bIndex.toHexString()}")
        }
    }

    @Test
    fun testToInt(){
        val bIndex = byteArrayOf(0x5 ,0x31)
        println("$bIndex hex ==> ${bIndex.toHexString()}  有符号 Int:${bIndex.readInt16BE()} ")
        println("$bIndex hex ==> ${bIndex.toHexString()}  无符号 Int:${bIndex.readUInt16BE() }")
    }

    @Test
    fun testByte(){
        val num = ByteArray(2)
        num[0] = 0xff.toByte()
        num[1] = 0xff.toByte()
        println("num:${num.readInt16BE()}  to Hex: ${num.toHexString()}")
    }


    @Test
    fun testAdd(){
        val a = 1
        val b =  10
        println("$a + $b = ${a+ b} : hex(${(a+b).to2ByteH().toHexString()})")
    }

    @Test
    fun testHe(){
        val num = 0x022B
        println("num(HEX):${num.to2ByteH().toHexString()}   int : $num")

        val num1 = 0x0005
        println("num1(HEX):${num1.to2ByteH().toHexString()}   int : $num1")
    }

}